<?php
apf_require_controller("Abstract");
class Ajax_UpdateRoleController extends AbstractController {

    private $bll_role;

    public function handle_request() {
        //检测用户权限
        if(!$this->check_role_auth()){
            $this->json_encode(0,Const_Tip::ERROR_AUTH);
        }
        $params = $this->request->get_parameters();
        $this->bll_role = new Bll_Flow_Role();
        $this->_check_params($params);

        $type = trim($params['type']);
        unset($params['type']);

        if( $type == 'add' ){
            $this->_add_role(intval($params['role_type']), intval($params['user_id']), intval($params['city_id']), trim($params['user_info']));
        }elseif( $type == 'del' ){
            $this->_delete_role($params['role_id']);
        }else{
            $this->json_encode(0,Const_Tip::ERROR_JOB_DB);
        }
    }
    /**
     * 删除角色
     * @param int $role_id 角色自增ID
     */
    private function _delete_role($role_id){
        $re = $this->bll_role->delete_role($role_id);
        if($re){
            $this->json_encode(1,$re);
        }else{
            $this->json_encode(0,Const_Tip::DEL_ROLE_ERROR);
        }
    }
    /**
     * 添加角色
     * @param unknown_type $role_type
     * @param unknown_type $user_id
     * @param unknown_type $city_id
     * @param unknown_type $user_info
     */
    private function _add_role($role_type, $user_id, $city_id, $user_info){
        $re = $this->bll_role->add_role($role_type, $user_id, $city_id, $user_info);
        if($re){
            $this->json_encode(1,$re);
        }else{
            $this->json_encode(0,Const_Tip::ADD_ROLE_ERROR);
        }
    }

    /**
     * 参数检测
     */
    private function _check_params($params){
        $status = 0;
        if($params['type'] == 'add'){
            if(!is_numeric($params['city_id']) || !is_numeric($params['user_id']) || !is_numeric($params['role_type']) || !isset($params['user_info'])){
                $this->json_encode($status,Const_Tip::ERROR_PARAM);
            }
            //检查城市ID
            apf_require_class("Bll_Hr_Property");
            $bll_pro = new Bll_Hr_Property();
            $city_list = $bll_pro->get_city_list();
            if(!array_key_exists($params['city_id'], $city_list)){
                $this->json_encode($status,Const_Tip::ERROR_PARAM);
            }
            //检查角色类型
            apf_require_class("Const_Flow");
            $role_list = Const_Flow::role_list();
            if(!array_key_exists($params['role_type'], $role_list)){
                $this->json_encode($status,Const_Tip::ERROR_PARAM);
            }
            //检查该角色是否已存在
            $re = $this->bll_role->get_role_list($params['city_id'], $params['role_type'], $params['user_id']);
            if(is_array($re)){
                if($re){
                    $this->json_encode($status,Const_Tip::REPEAT_ROLE);
                }
            }else{
                $this->json_encode($status,Const_Tip::REPEAT_ROLE);
            }

        }elseif($params['type'] == 'del'){
            if( !is_numeric($params['role_id']) ){
                $this->json_encode($status,Const_Tip::ERROR_PARAM);
            }
        }else{
            $this->json_encode($status,Const_Tip::ERROR_PARAM);
        }
    }
}